﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// 求多项式命名空间
/// </summary>
namespace ClassPolynomial
{
    /// <summary>
    /// 求一维多项式类
    /// </summary>
    public class Polynomial1D
    {
        private double[] a = null;  //多项式系数
        private int n;   //多项式的项的个数
        private double x; //自变量


        public Polynomial1D()
        {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="parameter"></param> 多项式系数
        /// <param name="argument"></param>  自变量
        public Polynomial1D(double[] parameter, double argument)
        {

            n = InitPara(parameter); ;
            x = argument;

        }
        /// <summary>
        /// 多项式系数初始化函数
        /// </summary>
        /// <param name="parameter"></param>多项式系数
        /// <returns></returns>返回系数的个数
        public int InitPara(double[] parameter)
        {
            a = (double[])parameter.Clone();
            return a.Length;
        }
        public int N
        {
            get
            {
                return n;
            }
            set
            {
                n = value;
            }
        }

        public double X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        public double this[int index]
        {
            get
            {
                return a[index];
            }
            set
            {
                a[index] = value;
            }
        }

        /// <summary>
        /// 求一维多项式
        /// </summary>
        /// <returns></returns>返回求得的结果
        public double polynomial1DResult()
        {
            int i;
            double result;
            result = a[n - 1];
            for (i = n - 2; i >= 0; i--)
            {
                result = result * x + a[i];
            }
            return result;
        }

    }

    public class Polynomial2D
    {
        private double[,] a;
        public int M
        { get; set; }
        public int N
        { get; set; }
        public double X
        { get; set; }
        public double Y
        { get; set; }
        public double this[int x, int y]
        {
            get
            {
                return a[x, y];
            }
            set
            {
                a[x, y] = value;
            }
        }

        public Polynomial2D(double[,] parameter, double x, double y)
        {
            InitPara(parameter);
            X = x;
            Y = y;
        }

        public int InitPara(double[,] parameter)
        {
            a = (double[,])parameter.Clone();
            M = a.GetLength(0);
            N = a.GetLength(1);
            return 0;
        }

        public double Polynomial2DResult()
        {
            int i, j;
            double result = 0.0, temp, x_i=1.0;
            for(i = 0; i< M; i++)
            {
                temp = a[i,N-1] * x_i;
                for(j=N-2; j>=0; j--)
                {
                    temp = temp * Y + a[i, j] * x_i; 
                }
                result += temp;
                x_i *= X; 
            }
            return result;
        }
    }
}
